www.gusucode.com > VC++ 酒店管理系统-源码程序 > VC++ 酒店管理系统-源码程序/code/hotel2003/ChooseFood.cpp
// ChooseFood.cpp : implementation file // Download by http://www.NewXing.com #include "stdafx.h" #include "qq.h" #include "ChooseFood.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CChooseFood dialog CChooseFood::CChooseFood(CWnd* pParent /*=NULL*/) : CDialog(CChooseFood::IDD, pParent) { //{{AFX_DATA_INIT(CChooseFood) m_PaidType = -1; //}}AFX_DATA_INIT m_DefaultSum=0.00f; bStriked=false; } void CChooseFood::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CChooseFood) DDX_Control(pDX, IDC_PAID, Paid); DDX_Control(pDX, IDC_SUM, Sum); DDX_Control(pDX, IDC_REBATE, Rebate); DDX_Control(pDX, IDC_FOODBILLLIST, FoodBillList); DDX_Control(pDX, IDC_FOODNUM, FoodNum); DDX_Control(pDX, IDC_FOODLIST, FoodList); DDX_Control(pDX, IDC_BILLCODE, BillCode); DDX_Control(pDX, IDC_HANDCODE, HandCode); DDX_Radio(pDX, IDC_RADIO1, m_PaidType); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CChooseFood, CDialog) //{{AFX_MSG_MAP(CChooseFood) ON_BN_CLICKED(IDC_ADD, OnAdd) ON_BN_CLICKED(IDC_DELETE, OnDelete) ON_BN_CLICKED(IDC_UPDATEDATA, OnUpdatedata) ON_BN_CLICKED(IDC_RADIO1, OnRadio) ON_BN_CLICKED(IDC_RADIO2, OnRadio) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CChooseFood message handlers BOOL CChooseFood::OnInitDialog() { CDialog::OnInitDialog(); HICON m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME2); this->SetIcon(m_hIcon,true);//设置对话框图标 // TODO: Add extra initialization here if(!m_FoodInfoSet.Open()) { MessageBox("打开数据库失败!","数据库错误",MB_OK); return false; } m_FoodInfoSet.MoveFirst(); while (!m_FoodInfoSet.IsEOF()) { CString strFoodList="",strFoodPrice=""; strFoodList=m_FoodInfoSet.m_FoodName; strFoodList=strFoodList+" "; strFoodPrice.Format("%5.0f",m_FoodInfoSet.m_FoodPrice); strFoodList=strFoodList+strFoodPrice; FoodList.AddString(strFoodList); m_FoodInfoSet.MoveNext(); } m_FoodInfoSet.Close(); //产生帐单号码 CCreateBillCode billcode; BillCode.SetWindowText(billcode.GenereBillCode()); //将钱的总数设置为0 Sum.SetWindowText("0"); ///Paid无效话 Paid.SetWindowText("0"); Paid.EnableWindow(FALSE); //设置缺省折扣率 Rebate.SetWindowText("1"); bStriked=false; return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CChooseFood::OnAdd() { // TODO: Add your control notification handler code here //////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// ///////////////////////添加一个项目模块 int nCurSel=FoodList.GetCurSel(); if(nCurSel==-1) { MessageBox("请选择至少一种菜!","选择错误",MB_OK); return; } CString strFoodNum; FoodNum.GetWindowText(strFoodNum); if(strFoodNum==""||strFoodNum=="0") { MessageBox("数量至少要为一","选择错误",MB_OK); return; } CString strFoodName=""; FoodList.GetText(nCurSel,strFoodName);//得到文本 char* s=strFoodName.GetBuffer(strFoodName.GetLength()); char* str=strtok(s," ");//得到菜的名字 bool bSame=false; for(int i=0;i<FoodBillList.GetCount();i++) { CString strTemp=""; FoodBillList.GetText(i,strTemp);//得到文本 char* temp=strTemp.GetBuffer(strTemp.GetLength());//得到缓冲区字符 char* bSameText=strtok(temp," "); if(*bSameText==*str)//找到一个相同的项目 { bSame=true; MessageBox("你已经选了这个食品!","选择错误",MB_OK); return; } // strTemp.ReleaseBuffer(-1); } if(!bSame)//若没有相同的项目则添加新的记录 { strFoodName=str;//构造显示文本 strFoodName=strFoodName+" "+strFoodNum; FoodBillList.AddString(strFoodName);//添加字符窜 } /////////////////////添加一个项目完成 //////////////////////////////////////////////////////////////////// /*########################计算当前应付的总款######################### ####################################################################*/ float MoneySum=0;//总钱数目 for( i=0;i<FoodBillList.GetCount();i++) { //取得当前文本 CString BillListText=""; FoodBillList.GetText(i,BillListText);//取得文本 //将取得的文本进行转换(char*) char* billlistText=BillListText.GetBuffer(BillListText.GetLength()); char* FoodNameInBillList,*FoodNumInBillList; //分别取得对应的数据 FoodNameInBillList=strtok(billlistText," ");//食物名字 FoodNumInBillList=strtok(NULL," "); //食物数量 //根据菜单的名字查找相应的价钱 CString SQLstr; SQLstr="select * from FoodInfo where FoodName='"; SQLstr=SQLstr+FoodNameInBillList; SQLstr=SQLstr+"'"; if(!m_FoodInfoSet.Open(AFX_DB_USE_DEFAULT_TYPE,SQLstr)) { MessageBox("打开数据库失败!","数据库错误",MB_OK); return; } float nMoney=m_FoodInfoSet.m_FoodPrice;//得到价钱 m_FoodInfoSet.Close();//关闭数据库 ////////////////////////////////////////////////////////////////////////// ///计算总数 long FoodNum=0; FoodNum=atoi(FoodNumInBillList);//食物数量 MoneySum=MoneySum+nMoney*FoodNum; } ////将计算得到的总数再乘以折扣率 m_DefaultSum=MoneySum;//设置缺省的总数 float rebate=0.00f; CString strText; Rebate.UpdateData(true); Rebate.GetWindowText(strText); if(strText=="") { MessageBox("请输入折扣率!","数据为空错误",MB_OK); return; } char* sTemp=strText.GetBuffer(strText.GetLength()); rebate=atof(sTemp); MoneySum=m_DefaultSum*rebate;//乘以折扣率 CString strSum=""; strSum.Format("%6.00f",MoneySum); Sum.SetWindowText(strSum);//显示总数 //#################################################################### } void CChooseFood::OnDelete() { // TODO: Add your control notification handler code here int nCount=FoodBillList.GetCount(); if(nCount==0) {//当前记录为空 MessageBox("当前没有删除的项目!","记录为空错误",MB_OK); Sum.SetWindowText("0"); return; } int nCurSel=FoodBillList.GetCurSel(); if(nCurSel==-1) {//没有选择一种菜 MessageBox("你应该选择一种要删除的菜!","数据为空",MB_OK); return; } else { FoodBillList.DeleteString(nCurSel); } /////删除以后从新计算帐总数 int nNum=FoodBillList.GetCount(); float nMoney=0.00f; for(int i=0;i<nNum;i++) { CString strItemText=""; FoodBillList.GetText(i,strItemText);//得到项目文本 char* str=strItemText.GetBuffer(strItemText.GetLength()); char* strFoodName,*strFoodNum; strFoodName=strtok(str," ");//得到菜的名字 strFoodNum=strtok(NULL," ");//得到菜的数目 CString SQLstr; SQLstr="select * from FoodInfo where FoodName='"; SQLstr=SQLstr+strFoodName; SQLstr=SQLstr+"'"; if(!m_FoodInfoSet.Open(AFX_DB_USE_DEFAULT_TYPE,SQLstr)) { MessageBox("打开数据库失败!","数据库错误",MB_OK); return; } float nPrice=m_FoodInfoSet.m_FoodPrice;//得到价钱 m_FoodInfoSet.Close();//关闭数据库 //////计算总数 long nFoodNum=atoi(strFoodNum); nMoney=nMoney+nPrice*nFoodNum; } //显示更新后的总数 m_DefaultSum=nMoney;//设置缺省的总数 CString strShowText,strReabte; Rebate.GetWindowText(strReabte); char* s=strReabte.GetBuffer(strReabte.GetLength()); double n=atof(s); strShowText.Format("%6.00f",m_DefaultSum*n); Sum.SetWindowText(strShowText); } void CChooseFood::OnUpdatedata() { // TODO: Add your control notification handler code here ////将计算得到的总数再乘以折扣率 float MoneySum=0.00f; float rebate=0.00f; CString strText; Rebate.GetWindowText(strText);//得到折扣率文本 if(strText=="") { MessageBox("请输入折扣率!","数据为空错误",MB_OK); return; } char* sRebateTemp=strText.GetBuffer(strText.GetLength()); rebate=atof(sRebateTemp);//转变折扣率 MoneySum=m_DefaultSum*rebate; CString strSum=""; strSum.Format("%6.00f",MoneySum); Sum.SetWindowText(strSum);//显示总数 } void CChooseFood::OnRadio() { // TODO: Add your control notification handler code here this->UpdateData(true); if(m_PaidType==0) { bStriked=true; Paid.EnableWindow(true); } if(m_PaidType==1) { bStriked=false; Paid.EnableWindow(false); } } void CChooseFood::OnOK() {//保存数据到数据库中 // TODO: Add extra validation here OnUpdatedata();//自动更新数据,防止因为忘记更新或者其他的原因产生误操作 CString strHandCode; HandCode.GetWindowText(strHandCode); if(strHandCode=="") { MessageBox("请输入手牌号!","数据为空错误",MB_OK); return; } /////如果选择的是"现结"方式,而已经付的金额为<0则提示输入错误 CString strPaidTemp=""; Paid.GetWindowText(strPaidTemp);//得到已经付的款 if(bStriked) { if(strPaidTemp==""||strPaidTemp=="0") { MessageBox("你可能输入错误,客人已经付的款应该大于0!!","输入错误",MB_OK); return; } } //查找客人数据库中是否存在这个手牌号 if(!m_GuestInfoSet.Open(AFX_DB_USE_DEFAULT_TYPE)) { MessageBox("打开数据库失败!","数据库错误",MB_OK); return; } m_GuestInfoSet.MoveFirst(); bool bhas=false; while(!m_GuestInfoSet.IsEOF()) { if(m_GuestInfoSet.m_HandCode==strHandCode) { ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////// bhas=true;//存在这条记录 break; } m_GuestInfoSet.MoveNext();//移动到下一条记录 } if(!bhas) { MessageBox("你输入的手牌号不正确!","输入消息错误",MB_OK); m_GuestInfoSet.Close();//关闭数据库 return; } m_GuestInfoSet.Close();//关闭数据库 ////////查询完毕 ////////////////////////////////////////////////////////////////////////// /////将数据保存到相应的数据库中 /////写BillInfoSet表 if(!m_BillInfoSet.Open()) {//打开数据库失败处理异常 MessageBox("打开数据库失败!","数据库错误",MB_OK); return; } /////新增加一条帐单的记录 m_BillInfoSet.AddNew(); /////数据填充 CString strBillCode,strSum,strPaid,strRebate; BillCode.GetWindowText(strBillCode); m_BillInfoSet.m_BillCode=strBillCode;//帐单号码 m_BillInfoSet.m_HandCode=strHandCode;//客人手牌 Sum.GetWindowText(strSum); char* s=strSum.GetBuffer(strSum.GetLength()); long nSum=atol(s); m_BillInfoSet.m_Sum=nSum;//应付总数 Paid.GetWindowText(strPaid); s=strPaid.GetBuffer(strPaid.GetLength()); long nPaid=atol(s); m_BillInfoSet.m_Paid=nPaid;//实付总数 Rebate.GetWindowText(strRebate); m_BillInfoSet.m_Rebate=strRebate;//折扣率 m_BillInfoSet.m_Striked=bStriked;//是否已经付款 m_BillInfoSet.Update();//数据更新 m_BillInfoSet.Close(); //关闭数据库 ////////////////////////////////////////////////////////////////////////// // 开始写BillContentInfo表,记录这个帐单的具体消费情况 if(!m_BillContentInfoSet.Open()) { MessageBox("打开数据库失败!","数据库错误",MB_OK); return; } m_BillContentInfoSet.AddNew();//新增一条记录 CString strBillContent=""; for(int i=0;i<FoodBillList.GetCount();i++) { CString strTemp=""; FoodBillList.GetText(i,strTemp); strBillContent=strBillContent+strTemp+","; } CString strBillcode; BillCode.GetWindowText(strBillcode); m_BillContentInfoSet.m_BillCode=strBillcode; m_BillContentInfoSet.m_BillContent=strBillContent; m_BillContentInfoSet.Update();//更新数据库,将缓冲区的数据写入数据库中 m_BillContentInfoSet.Close();//关闭数据库 CDialog::OnOK(); } void CChooseFood::OnCancel() { // TODO: Add extra cleanup here CDialog::OnCancel(); }